import {defineStore} from "pinia";
import {model, msg, request} from "@ut/tool";

export const healthStore = defineStore('healthStore', {
    state: () => ({
        loading: false,
        list: [],
        total: 0,
        hsList: [],
        hsTotal: 0
    }),
    actions: {
        // 查询
        get(params) {
            return new Promise(async t => {
                this.loading = true;
                let {data, total} = await request({url: '/hr/get', method: 'get', params});
                this.list = data;
                this.total = total;
                this.loading = false;
                t();
            })
        },

        // 添加
        create(data) {
            return new Promise(async t => {
                this.loading = true;
                await request({url: '/hr/create', method: 'post', data});
                msg('添加成功');
                t();
            })
        },

        // 修改
        update(data) {
            return new Promise(async t => {
                this.loading = true;
                await request({url: '/hr/update', method: 'put', data});
                msg('修改成功');
                t();
            })
        },

        // 删除
        remove(id) {
            return new Promise(async t => {
                this.loading = true;
                let res = await model('提示', '正在删除卫生检查记录，是否继续？', 'warning');
                if (res) {
                    await request({url: '/hr/remove', method: 'delete', params: {id}});
                    msg('删除成功');
                    t();
                }
            })
        },

        // 查看卫生评分
        getHs(params) {
            return new Promise(async t => {
                let {data, total} = await request({url: '/hs/get', method: 'get', params});
                this.hsList = data;
                this.hsTotal = total;
                this.hsList.forEach(e => {
                    e.rating = '优秀';
                    if (e.grade < 80) e.rating = '良好';
                    if (e.grade < 60) e.rating = '不及格';
                })
                t();
            })
        },

        // 更新评分
        updateHs(data) {
            return new Promise(async t => {
                await request({url: '/hs/update', method: 'put', data});
                msg('填写成功');
                t();
            })
        },
    }
})